import shutil
import time
from pathlib import Path


def list_files_with_pathlib(folder_path):
    try:
        folder = Path(folder_path)
        if not folder.exists():
            print(f"错误：目录 '{folder_path}' 不存在。")
            return []
        if not folder.is_dir():
            print(f"错误：'{folder_path}' 不是一个目录。")
            return []

        # 获取所有文件（不包括子目录）
        files = [file.name for file in folder.iterdir() if file.is_file()]
        return files
    except PermissionError:
        print(f"错误：没有权限访问目录 '{folder_path}'。")
        return []


def get_subfolders(path):
    p = Path(path)
    # 只获取第一层子文件夹
    return [f for f in p.iterdir() if f.is_dir()]


if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    folder_path = "D:\\3\\"
    folders = get_subfolders(folder_path)
    for f in folders:
        image_1 = []
        folder_path = str(f)
        files = list_files_with_pathlib(folder_path)
        for file_1 in files:
            image_1.append(file_1[0:-8])
        unique_arr = list(set(image_1))
        for aa in unique_arr:
            temp = 0
            for file_1 in files:
                if file_1.startswith(aa):
                    temp += 1
            if temp == 1:
                file_name = aa + "0000.png"
                src = Path(str(f) + "\\" + file_name)
                dst = Path(str(src).replace('\\3\\', '\\4\\1\\1\\'))
                dst.parent.mkdir(parents=True, exist_ok=True)
                shutil.move(str(src), str(dst))
        for aa in unique_arr:
            temp = 0
            wei = 0
            for file_1 in files:
                if file_1.startswith(aa):
                    bb = int(file_1[-8:-7])
                    cc = int(file_1[-7:-4])
                    if bb > temp:
                        temp = bb
                    if cc > wei:
                        wei = cc
            if temp == 0:
                if wei == 1:
                    for file_1 in files:
                        if file_1.startswith(aa):
                            src = Path(str(f) + "\\" + file_1)
                            dst = Path(str(src).replace('\\3\\', '\\4\\1\\2\\'))
                            dst.parent.mkdir(parents=True, exist_ok=True)
                            shutil.move(str(src), str(dst))
                elif wei == 2:
                    for file_1 in files:
                        if file_1.startswith(aa):
                            src = Path(str(f) + "\\" + file_1)
                            dst = Path(str(src).replace('\\3\\', '\\4\\1\\3\\'))
                            dst.parent.mkdir(parents=True, exist_ok=True)
                            shutil.move(str(src), str(dst))
                elif wei == 3:
                    for file_1 in files:
                        if file_1.startswith(aa):
                            src = Path(str(f) + "\\" + file_1)
                            dst = Path(str(src).replace('\\3\\', '\\4\\1\\4\\'))
                            dst.parent.mkdir(parents=True, exist_ok=True)
                            shutil.move(str(src), str(dst))
                elif wei == 4:
                    for file_1 in files:
                        if file_1.startswith(aa):
                            src = Path(str(f) + "\\" + file_1)
                            dst = Path(str(src).replace('\\3\\', '\\4\\1\\5\\'))
                            dst.parent.mkdir(parents=True, exist_ok=True)
                            shutil.move(str(src), str(dst))
                elif wei >= 5:
                    for file_1 in files:
                        if file_1.startswith(aa):
                            src = Path(str(f) + "\\" + file_1)
                            dst = Path(str(src).replace('\\3\\', '\\4\\1\\6\\'))
                            dst.parent.mkdir(parents=True, exist_ok=True)
                            shutil.move(str(src), str(dst))
            elif temp == 1:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\2\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 2:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\3\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 3:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\4\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 4:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\5\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 5:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\6\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 6:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\7\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp == 7:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\8\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
            elif temp >= 8:
                for file_1 in files:
                    if file_1.startswith(aa):
                        src = Path(str(f) + "\\" + file_1)
                        dst = Path(str(src).replace('\\3\\', '\\4\\9\\'))
                        dst.parent.mkdir(parents=True, exist_ok=True)
                        shutil.move(str(src), str(dst))
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
